package com.framework.base.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Title: WeekUtil.java
 * @Package com.framework.base.util
 * @Description:计算周 * @jdk version 1.6
 * @author www.51wuxia.com
 */
public class WeekUtil {
	/**
	 * 取得当前日期是多少周
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekOfYear(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setMinimalDaysInFirstWeek(7);
		c.setTime(date);

		return c.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 得到某一年周的总数
	 * 
	 * @param year
	 * @return
	 */
	public static int getMaxWeekNumOfYear(int year) {
		Calendar c = new GregorianCalendar();
		c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

		return getWeekOfYear(c.getTime());
	}

	/**
	 * 得到某年某周的第一天
	 * 
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getFirstDayOfWeek(int year, int week) {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, 1);

		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, week * 7);

		return getFirstDayOfWeek(cal.getTime());
	}

	/**
	 * 得到某年某周的最后一天
	 * 
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getLastDayOfWeek(int year, int week) {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DATE, 1);

		Calendar cal = (GregorianCalendar) c.clone();
		cal.add(Calendar.DATE, week * 7);

		return getLastDayOfWeek(cal.getTime());
	}

	/**
	 * 取得当前日期所在周的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		return c.getTime();
	}

	/**
	 * 取得当前日期所在周的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		return c.getTime();
	}

	public static Map<String, String> getWeekDays(Date date) {
		Map<String, String> weekMap = new TreeMap<String, String>();
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		for (int i = 0; i <= 6; i++) {
			c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + i); // Sunday
			String dateStr = DateUtil.dateFormat(c.getTime(),
					"yyyy-MM-dd HH:mm:ss");
			weekMap.put(dateStr, getWeekNum().get(i).toString());
		}
		return weekMap;
	}

	public static Map<Integer, String> getWeekNum() {
		Map<Integer, String> weekNum = new HashMap<Integer, String>();
		weekNum.put(0, "一");
		weekNum.put(1, "二");
		weekNum.put(2, "三");
		weekNum.put(3, "四");
		weekNum.put(4, "五");
		weekNum.put(5, "六");
		weekNum.put(6, "日");
		return weekNum;
	}

	public static void main(String[] args) {
		Map cc = new HashMap();
		cc.put("1", "33");
		cc.put("1", "44");
		cc.put("2", "5");
		cc.put("2", "6");
		System.out.println(""+cc.get("2"));
	}

}
